// ActionScript file

package zakladki.AS3 {
import AS3.constants.combo.VehicleType;

import flash.system.Capabilities;

import mx.collections.*;
import mx.core.Application;
import mx.core.FlexGlobals;

public class PrivateTransportChangeListener {

    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;
    private var vehicleControlsList:Array = new Array();

    public function PrivateTransportChangeListener(vehicleControls:Array) {
        this.vehicleControlsList = vehicleControls;
    }

    public function vehicleTypeChanged(number:int,calc:Boolean):void {
        doChangeVehicleView(number);
        var vehicleSelectedData:VehicleSelectedData = createVehicleSelectedData(number);
        var vehicleType:int = vehicleSelectedData.vehicleType;
        mainApp.widok_sceny.changeVehicleVisibility(number,vehicleType);
        if(calc){
           updatePerson(number,vehicleSelectedData);
           mainApp.calculate();
        }
    }

    public function vehiclePropertiesChanged(number:int,calc:Boolean):void {
        if(calc){
           var vehicleSelectedData:VehicleSelectedData = createVehicleSelectedData(number);
           updatePerson(number,vehicleSelectedData);
           mainApp.calculate();
        }
    }

    public function doChangeVehicleView(number:Number):void {
        var vehicleControls:VehicleControls = vehicleControlsList[number] as VehicleControls;
        this.setVehicleControlsVisibility(vehicleControls,false);

        var vehicleType:int = vehicleControls.vehicleTypeCombo.selectedItem.data;

        if(vehicleType == VehicleType.BICYCLE){
            vehicleControls.vehicleAgeCombo.visible = true;
        }

        if(vehicleType != VehicleType.NONE && vehicleType != VehicleType.BICYCLE){
            this.setVehicleControlsVisibility(vehicleControls,true);
            if(vehicleType != VehicleType.BIKE){
                vehicleControls.acEnabledCheckBox.visible = true;
            }
            switch(vehicleType){
                case VehicleType.BIKE:
                    vehicleControls.fuelConsumptionStepper.value = 5;
                    break;
                case VehicleType.SMALL_ENGINE:
                    vehicleControls.fuelConsumptionStepper.value = 5;
                    break;
                case VehicleType.MEDIUM:
                    vehicleControls.fuelConsumptionStepper.value = 8;
                    break;
                case VehicleType.BIG:
                    vehicleControls.fuelConsumptionStepper.value = 14;
                    break;
                case VehicleType.LARGE:
                    vehicleControls.fuelConsumptionStepper.value = 14;
                    break;
            }
        }
    }

    private function setVehicleControlsVisibility(vehicleControls:VehicleControls,visibility:Boolean):void {
           vehicleControls.fuelTypeCombo.visible = visibility;
           vehicleControls.vehicleAgeCombo.visible = visibility;
           vehicleControls.fuelConsumptionStepper.visible = visibility;
           vehicleControls.passengerFreqCombo.visible = visibility;
           vehicleControls.weekDistanceStepper.visible = visibility;
           vehicleControls.acEnabledCheckBox.visible = visibility;
       }


    private function createVehicleSelectedData(number:int):VehicleSelectedData{
        var vehicleControls:VehicleControls = vehicleControlsList[number] as VehicleControls;

        var vehicleType:int = vehicleControls.vehicleTypeCombo.selectedItem.data;
        var vehicleAge:int = vehicleControls.vehicleAgeCombo.selectedItem.data;
        var fuelType:int = vehicleControls.fuelTypeCombo.selectedItem.data;
        var secondPersonFreq:int = vehicleControls.passengerFreqCombo.selectedItem.data;
        var weekDistance:int = vehicleControls.weekDistanceStepper.value;
        var fuelConsumption:int = vehicleControls.fuelConsumptionStepper.value;
        var acEnabled:Boolean = vehicleControls.acEnabledCheckBox.selected;

        return new VehicleSelectedData(vehicleType,fuelType,secondPersonFreq,vehicleAge,fuelConsumption,weekDistance,acEnabled);
    }

    private function updatePerson(number:int,vehicleData:VehicleSelectedData):void {
        mainApp.privateTransportUpdate(number,vehicleData);
    }
}
}
